<template>
  <!--进度节点新增  -->
  <div >
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="1"
      :labelWidth="110"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div class="dialogBottmView">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { parseTime } from '@/utils/index';
import { insertDealerApplyPnode,updataDealerApplyPnode} from '@/api/cms-api/origin/construct/dealer';
export default {
  mixins: [dictionary],
  props: {
    value:{
        type: Object,
        default: null
    }
  },
  data() {
    return {
      initForm: this.initFormFun(),
      saveLoading: false,
      formData: {}
    };
  },
  mounted() {
    //如果传入参数不为空，则说明是修改进入，初始化对应数据
    if(this.value!=null && (this.value.type=="edit"||this.value.type=="show")){
      this.init();
    }else{
      //设置为当前时间
      //新增时，建档日期默认为当前日期
      this.initForm[1].initialValue = parseTime(new Date(),'{y}-{m}-{d} {h}:{i}:{s}');
      // var date = new Date();
      // this.initForm[1].initialValue = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() +' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
    }
  },
  methods: {
    //修改时，初始化页面数据
    init(){
      console.log("this.value.rowData:",this.value.rowData);
      //循环为表单赋值
      this.initForm.forEach(x => {
        if(x.fieldName == "fileUrl"){ //如果为附件
          let urlsList = this.value.rowData.urls;
          if(urlsList!=null && urlsList.length>0){
            let fileList = [];
            for(let i in urlsList){
                let fileName = { name: urlsList[i].fileName };
                let fileId = { url: urlsList[i].fileId };
                let f = Object.assign(fileName, fileId);
                fileList.push(f);
            }
            x.initialValue = fileList;
          }
        }
        else{
          x.initialValue = this.value.rowData[x.fieldName];
        }
        
      });
    },
    //搜索数据
    initFormFun() {
      return [
        {
          type: 'SELECT',
          label: '进度节点类型',
          fieldName: 'nodeType',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('2547'),
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'DATE',
          label: '时间',
          fieldName: 'nodeTime',
          valueFormat: 'yyyy-MM-dd HH:mm:ss', //返回数据格式
          initialValue: '',
          placeholder: '请选择',
          style: { width: '100%' },
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        },
        {
          type: 'UPLOAD_FILE',
          label: '附件',
          fieldName: 'fileUrl',
          upload: {
            limit:0,
            actionUrl: '/api/file/oss/upload',
            fileTypes: ['jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'pdf', 'rar', 'zip'],
            max:1024*30
          },
          initialValue: '',
          rules: [
            {
              required: true,
              message: '必填',
              trigger: 'change'
            }
          ]
        }
      ];
    },
    formChangeHandle(val) {
      this.formData = val;
      this.doSaveDialog();
    },
    //保存按钮
    saveDialog() {

      this.$refs.formPanel.SUBMIT_FORM();
    },
    //提交保存
    async doSaveDialog() {
      this.saveLoading = true;
      let fileUrlList = [];
      //获取附件，转为list类型
      for (let i = 0; i < this.formData.fileUrl.length; i++) {
        fileUrlList.push(Object.assign({},{"name":this.formData.fileUrl[i].name},{"url":this.formData.fileUrl[i].url}));
      }

      let submitData = Object.assign({},{"applyId":this.value.applyId},this.formData);
      submitData.filesInfo = fileUrlList;
      let res;
      //如果传入参数不为空并且为修改
      if(this.value!=null && this.value.type=="edit"){
        res = await updataDealerApplyPnode(submitData,this.value.rowData["applyPnodeId"]);
      }else{
        res = await insertDealerApplyPnode(submitData);
      }
      if (res.resultCode === 200) {
        this.$notify.success({title: '提示信息',message:'保存成功!'});
        // 重新加载
        this.closeDialog('refresh');
      }else {
        this.$notify.error({title: '提示信息',message:res.errMsg||'保存失败!'});
        this.closeDialog();
      }
    },
    //取消操作
    closeDialog(type) {
      this.$emit('close', { visible: false,type:type});
    }
  }
};
</script>